﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.IO;
using LyData.Data;

public partial class Admin_AdCreateJS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string adurl = "";
        string width = "";
        string height = "";
        string picurl = "";
        string exp = "";
        string jsname = "";
        string AdType = "";
        IDb db = new DbFactory().DriverDb();
        db.Open();
        DataTable table = db.DataTable("*", "Ad", "1=1", "");
        db.Close();
        int count = 0;
        for (int i = 0; i < table.Rows.Count; i++)
        {
            adurl = table.Rows[i]["AdUrl"].ToString();
            width = table.Rows[i]["AdWidth"].ToString();
            height = table.Rows[i]["AdHeight"].ToString();
            picurl = table.Rows[i]["AdAddr"].ToString();
            exp = table.Rows[i]["AdExp"].ToString();
            jsname = table.Rows[i]["AdName"].ToString();
            AdType = table.Rows[i]["AdType"].ToString();
            CreateJs(adurl, width, height, picurl, exp, jsname, Convert.ToInt16(AdType));
            count = i + 1;
        }
        Response.Write("<script>alert('生成成功，共生成" + count.ToString() + "个JS');location.href='Ad.aspx';</script>");
    }

    /// <summary>
    /// 生成JS调用广告
    /// </summary>
    /// <param name="adurl"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <param name="picurl"></param>
    /// <param name="exp"></param>
    /// <param name="jsname"></param>
    /// <param name="adtype">1：图片 2：Flash 3：幻灯片 4：对联</param>
    public void CreateJs(string adurl, string width, string height, string picurl, string exp, string jsname, int adtype)
    {
        //先创建文件
        string path = "";
        path = Server.MapPath("~") + "\\Include" + "\\Ad\\" + jsname + ".JS";

        StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.UTF8);
        string flash = "<a href='" + adurl + "' target='_blank'><object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" width='" + width + "' height='" + height + "'>";
        string stem = "";
        if (adtype == 3)  //3：幻灯片
        {
            stem += "<!-- \n";
            stem += "var focus_width=" + width + ";  \n ";
            stem += "var focus_height=" + height + "; \n";
            stem += "var text_height=0; \n";
            stem += "var swf_height = focus_height+text_height; \n";
            stem += "var pics = '" + picurl + "';\n";
            stem += "var links = '" + adurl + "'; \n";
            stem += "var texts = '" + exp + "'; \n";
            stem += "document.write('<object ID=\"focus_flas\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"'+ focus_width +'\" height=\"'+ swf_height +'\">'); \n";
            stem += "document.write('<param name=\"allowScriptAccess\" value=\"sameDomain\"><param name=\"movie\" value=\"include/playswf.swf\"><param name=\"quality\" value=\"high\"><param name=\"bgcolor\" value=\"#FFFFFF\">'); \n";
            stem += "document.write('<param name=\"menu\" value=\"false\"><param name=wmode value=\"opaque\">'); \n";
            stem += "document.write('<param name=\"FlashVars\" value=\"pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'\">'); \n";
            stem += "document.write('<embed ID=\"focus_flash\" src=include/playswf.swf\" wmode=\"opaque\" FlashVars=\"pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'\" menu=\"false\" bgcolor=\"#C5C5C5\" quality=\"high\" width=\"'+ focus_width +'\" height=\"'+ swf_height +'\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />');\n		document.write('</object>'); \n";
            stem += "//-->";
        }
        string stemDL = "";
        if (adtype == 4)//4：对联
        {
            if (picurl != null)
            {
                string pic = picurl.Substring((picurl.Length - 1), 1);
                if (pic == "|")
                {
                    //去掉最后个|
                    pic = picurl.Substring(0, (picurl.Length - 1));
                }
                //得到数组
                string[] img = null;
                img = ret_Power(pic);
                int imgnum = img.Length;
                //采用平分广告
                int temp_pic_left = 0;//左边多少个
                int temp_pic_right = 0;//右边多少个
                if (imgnum % 2 == 0)
                {
                    if (imgnum >= 2)
                    {
                        temp_pic_left = imgnum / 2;
                        temp_pic_right = imgnum / 2;
                    }
                    else
                    {
                        temp_pic_left = imgnum;
                    }
                }
                else
                {
                    if (imgnum >= 2)
                    {
                        temp_pic_left = (imgnum / 2) + 1;
                        temp_pic_right = (imgnum / 2) - 1;
                    }
                    else
                    {
                        temp_pic_left = imgnum;
                    }
                }

                stemDL += "      //常数定义  \n ";
                stemDL += "self.onError=null;  \n";
                stemDL += " currentX   =   currentY   =   0;  \n";
                stemDL += "whichIt   =   null;  \n";
                stemDL += " lastScrollX   =   0;   //最后离左边距离的负值  \n";
                stemDL += " lastScrollY   =0;     //最后离顶部的高度的负值  \n";
                stemDL += "//----------------------start   fun秒执行一次 \n";
                stemDL += "function   heartBeat(id1,id2)   {          \n";
                stemDL += "diffY   =   document.body.scrollTop; \n";
                stemDL += " diffX   =   document.body.scrollLeft; \n";
                stemDL += " if(diffY   !=   lastScrollY)   {       \n";
                stemDL += "percent   =   .1   *   (diffY   -   lastScrollY);  \n";
                stemDL += "if(percent   >   0)   percent   =   Math.ceil(percent);   \n";
                stemDL += " else   percent   =   Math.floor(percent);   \n";
                stemDL += " id1.style.pixelTop   +=   percent; \n";
                stemDL += "id2.style.pixelTop   +=   percent; \n";
                stemDL += "lastScrollY   =   lastScrollY   +   percent;  \n";
                stemDL += " }     ";
                stemDL += "if(diffX   !=   lastScrollX)   {     \n";
                stemDL += " percent   =   .1   *   (diffX   -   lastScrollX); \n";
                stemDL += "if(percent   >   0)   percent   =   Math.ceil(percent);  \n";
                stemDL += " else   percent   =   Math.floor(percent); \n";
                stemDL += "id1.style.pixelTop   +=   percent;   \n";
                stemDL += " id2.style.pixelTop   +=   percent; \n";
                stemDL += " lastScrollX   =   lastScrollX   +   percent;    \n";
                stemDL += "  } \n";
                stemDL += " } \n";
                stemDL += "//-----------------------end   fun   \n";
                stemDL += "scr=screen.width \n";
                stemDL += "left_1=(scr>800)?6:145   \n";
                stemDL += "right_1=(scr>840)?920:580  \n";
                //左侧图片   
                stemDL += "document.write(\"<DIV   id=f1   style='left:   \"+left_1+\"px;   top:   215px;   POSITION:   absolute;'><table>";
                for (int i = 0; i < temp_pic_left; i++)
                {
                    stemDL += "<tr><td><img src='" + img[i] + "' border=0 width='70' height='70'></td></tr>";
                }
                stemDL += "</table></div>\") \n";
                //右侧图片   
                stemDL += "document.write(\"<DIV   id=f2   style='left:   \"+right_1+\"px;   top:   215px;   POSITION:   absolute;'><table>";
                for (int j = temp_pic_left; j < imgnum; j++)
                {
                    stemDL += "<tr><td><img src='" + img[j] + "' border=0 width='70' height='70'></td></tr>";
                }
                stemDL += "</table></div>\") \n";
                stemDL += "action = window.setInterval(\"heartBeat(f1,f2)\", 50);  \n";
            }
        }

        switch (adtype)
        {
            case 1:
                sw.WriteLine("document.write(\"<a href='" + adurl + "' target='_blank'><img border=0 src='" + picurl + "' width='" + width + "' height='" + height + "' alt='" + exp + "'></a>\");");
                break;
            case 2:
                sw.Write("document.write(" + flash + ")");
                break;
            case 3:
                sw.Write(stem);
                break;
            case 4:
                sw.Write(stemDL);
                break;
        }
        sw.Flush();
        sw.Close();
    }


    /// <summary>
    /// 返回的数组
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public string[] ret_Power(string str)
    {
        string[] Dic = str.Split('|');

        return Dic;
    }
}
